<?php
/**
 * Abstract controller
 * 
 * @author paq
 *
 */
abstract class Controller_Abstract
{
	/**
	 * Controller name
	 * 
	 * @var string
	 */
	protected $_name;
	
	/**
	 * Controller helpers
	 * 
	 * @var array [1 => AbstractControllerHelper]
	 */
	protected $_helpers = array();
	
	public function __construct($name)
	{
		$this->_name = strtolower((string) $name);
		
		Log::info($this->_name . ' controller created');
	}
	
	protected function registerHelper(Controller_Helper_Abstract $helper)
	{
		if (array_key_exists($helper->getName(), $this->_helpers)) {
			return;
		}
		
		$this->_helpers[$helper->getName()] = $helper; 
	}
	
	/**
	 * Gets controller's name
	 * 
	 * @return string
	 */
	public function getName()
	{
		return $this->_name;
	}
	
	public function dispatch()
	{
		
	}
	
	protected function preDispatch()
	{
		
	}
	
	protected function postDispatch()
	{
		
	}
	
	/**
	 * Informs about unsupported action
	 * 
	 * @param $method
	 * @param $args
	 * @return void
	 * @throws Exception when called action is not defined/supported by controller
	 */
	public function __call($method, $args)
	{
		throw new Exception('Controller does not support action: ' . $method);
	}
	
	/**
	 * 
	 * @param $name
	 * @return AbstractControllerHelper
	 */
	protected function getHelper($name)
	{
		if (!array_key_exists($name, $this->_helpers)) {
			throw new Exception($name . ' helper is not registered in controller.');
		}
		
		return $this->_helpers[$name];
	}
} 